//logs.js
import {
  allProducts
} from '../../lib/server'

Page({
  data: {
    productList: [],
    page: 1,
    scrollHeight:0,
    is_have:true,
    is_fresh:false
  },
  onLoad: function () {
    // 获取屏幕高度
    var that = this
		wx.getSystemInfo({
			success: function (res) {
				that.setData({
					scrollHeight: res.windowHeight
				});
			}
		});
    this.getProductList()
  },

  // 下拉刷新
  pullDownRefresh() {
    this.setData({
      page: 1,
      productList:[],
      is_have:true
    }, () => {
      this.getProductList()
    })    
  },

  // 获取商品列表
  getProductList: function() {
    if(this.data.is_have){
      allProducts({
        type: 'all',
        page: this.data.page
      }, ({
        success: data => {
          wx.stopPullDownRefresh()
          console.log(data)
          if(data.list.data.length){
            this.setData({
              productList: this.data.productList.concat(data.list.data),
              page: this.data.page + 1,
              is_fresh:false
            })
          }else{
            this.setData({
              productList: this.data.productList.concat(data.list.data),
              page: this.data.page + 1,
              is_have:false,
              is_fresh:false
            })
          }
          
        }
      }))
    }
    
  }
})